package cn.com.ejiakanghu.web.alimini;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenPublicMessageSingleSendRequest;
import com.alipay.api.response.AlipayOpenPublicMessageSingleSendResponse;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 阿里消息模板通知
 *
 * @author houzhibin
 * @create 2018-07-22 12:39
 */
public class AliMessageUtil {
    private static Logger logger = LoggerFactory.getLogger(AliMessageUtil.class);
    public static String SERVICE_PHONE = "4006710500";
    public static String LIVE_ACCOUNT_APPID = "2016012601120525";
//    public static String LIVE_ACCOUNT_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCKHYmQuGv+lOmcGDOoySs6MAd4hqXZ8E3lrmcVbOSfMBJiHLErL77pKv5iN9+nUZNyQB59sIvVctJ8TEmsvAJagSn09vDx79ieesnNO9VoqgM5z1mCOXZBOXVUqgyD2Kx8nmyWV4Wx/PwrihbYlKpOkD+/Uu1iBmsZBK7CXWIaIitTUSFtooNvoRF2cYbbypxC/8jrAR+cLv2YK6eyPiew4nyiKAyEhQGlxiX9ow99ivETawdrdPMK+oGnMeLFURgRKjOuZOI7teo285M8UphfVZmwZ1iW6EjF1rOICCXSJ6+AGsRQFMEkJdm+OSuvtWFoMI7rpT8fLydpPv3Um4WxAgMBAAECggEAMekYpE6w4A/qF7l7OZxw3Zcrq/dkicxYKg8LO2fdelVB32ik8YlQgSxDPzUXwnwtP1VCb4NUFO9odviOizhp1VDl25s9WQCmlCalNGBR73+4qLRwkSm4vTtJVi4ZVbAALQyHCxFXxAk7C3TcaqnWnYttLfOGyyysmR7jFGPwpqZ4YWI7jtmPHL1fX983api+U3ybRfbNe2WqdcN29ewbqPPNxjE0JpXNObTB3PdDpGp3tfU29EpNJq4FBleugTpBRD13yCGA4nqbFZxXztpxuZxUMNVI2wC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3LrQ4ol/Kf7ZVMbcw2/8LMPCU61x1GhBdm0ufmw2Iu3oi1SKR6hKqVx1BSCf+96wKBgE79H9djtixght9MYR1EMfNU79BsUkt09HrQdOu/GR427cmKAGpHIhdT5bvOYGaNnxjMhPda+N++r/5FapqX/u449ORhwTrDTSDtmOrC3qqQSnRRXgZabBXD5018ed8RYZfCybFmuseAGj4S+W2/dNbFZJhQG4Ujojn6YY8sXHHDAoGADoB4Lenf54i8+TKYMUqNvSOhreYmRdgoZgX8pjRutbxAJM5JK5tVliCxzjZ0QJMylHQHWXkqHCHDizch3cXRl1RW8L2xhe1U2O2iuxbeF3QGmRUmen9VZsk7oL8rCdtBd/levySwiaB37EsskCyrkZSP2Rns3IhZCuW5IcHjWUM=";
//    public static String LIVE_ACCOUNT_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAih2JkLhr/pTpnBgzqMkrOjAHeIal2fBN5a5nFWzknzASYhyxKy++6Sr+Yjffp1GTckAefbCL1XLSfExJrLwCWoEp9Pbw8e/YnnrJzTvVaKoDOc9Zgjl2QTl1VKoMg9isfJ5slleFsfz8K4oW2JSqTpA/v1LtYgZrGQSuwl1iGiIrU1EhbaKDb6ERdnGG28qcQv/I6wEfnC79mCunsj4nsOJ8oigMhIUBpcYl/aMPfYrxE2sHa3TzCvqBpzHixVEYESozrmTiO7XqNvOTPFKYX1WZsGdYluhIxdaziAgl0ievgBrEUBTBJCXZvjkrr7VhaDCO66U/Hy8naT791JuFsQIDAQAB";
    public static String LIVE_ACCOUNT_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApV4PklH7l3rpoeO+aZs+fhx34buMPR3W01gLI94l34D7fK0xi3NUNA+LZ91LA0zi3xDaJm42QlM1dPvMXY/BF002LDq/13PsArD5BPySiClN9nB78UzkfykrNp+ezcc8G2dg9bBYoadnNigE3z1NfsqPKIPE4YA0PcMcDCz29HUbFVHl+fRDvArwUHNtOWHoYxLhaDjK0U2IlaWgw3FJOgkFgB6hgkBWk/c8P1P1gHZtuyhjTY0ZtUjBo2ZxQLFMMT7RW82RIcqPBvx8rbhDycKNB5TfJPTKaOIGrsxSqfK6/kKLQnJUPpmErQDrw7JIVNQIMnvxE/2B7QiuC77cewIDAQAB";

    public static AlipayOpenPublicMessageSingleSendResponse sendNoticeMsg(String content) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",LIVE_ACCOUNT_APPID,LIVE_ACCOUNT_PRIVATE_KEY,"json","utf-8",LIVE_ACCOUNT_PUBLIC_KEY,"RSA2");
        AlipayOpenPublicMessageSingleSendRequest request = new AlipayOpenPublicMessageSingleSendRequest();
        request.setBizContent(content);
        AlipayOpenPublicMessageSingleSendResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }

    /**
     * 租赁成功消息发送
     * @param aliUid
     * @throws AlipayApiException
     */
    public static void sendLeaseSuccessMsg(String aliUid,String borrowTime,String borrowAddr,String borrowOrderCode) throws AlipayApiException {
        if(borrowAddr.length() > 20) {
            borrowAddr = borrowAddr.substring(0,20);
        }
        int orderLen = borrowOrderCode.length();
        if(orderLen > 20) {
            borrowOrderCode = borrowOrderCode.substring(orderLen - 20,orderLen);
        }
        String content = "{" +
                "\"to_user_id\":\""+aliUid+"\"," +
                "\"template\":{" +
                "\"template_id\":\"b0be1c087580405a85d120e68836866a\"," +
                "\"context\":{" +
                "\"head_color\":\"#85be53\"," +
                "\"url\":\"https://ejiakanghu.com.cn/guanwang/order_detail_desc.html\"," +
                "\"action_name\":\"查看详情\"," +
                "\"keyword1\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+borrowTime+"\"" +
                "        }," +
                "\"keyword2\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+borrowAddr+"\"" +
                "        }," +
                "\"keyword3\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+borrowOrderCode+"\"" +
                "        }," +
                "\"keyword4\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+SERVICE_PHONE+"\"" +
                "        }," +
                "\"first\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\"您好，您已经租借成功。\"" +
                "        }," +
                "\"remark\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\"祝您使用愉快。\"" +
                "        }" +
                "      }" +
                "    }" +
                "  }";
        AlipayOpenPublicMessageSingleSendResponse response = sendNoticeMsg(content);
        if(response.isSuccess()){
            logger.debug("leaseSuc调用成功,{}",borrowOrderCode);
        } else {
            System.out.println(JacksonUtil.gson.toJson(response));

            logger.error("leaseSuc调用失败,{}",borrowOrderCode);
        }
    }

    /**
     * 归还成功提醒
     * @param aliUid
     */
    public static void returnSuccessMsg(String aliUid,String borrowTime,String borrowAddr,String returnTime,String returnAddr,String borrowOrderCode,String num) throws AlipayApiException {
        if(borrowAddr.length() > 20) {
            borrowAddr = borrowAddr.substring(0,20);
        }
        if(returnAddr.length() > 20) {
            returnAddr = returnAddr.substring(0,20);
        }
        int orderLen = borrowOrderCode.length();
        if(orderLen > 20) {
            borrowOrderCode = borrowOrderCode.substring(orderLen - 20,orderLen);
        }
        String content = "{" +
                "\"to_user_id\":\""+aliUid+"\"," +
                "\"template\":{" +
                "\"template_id\":\"9251d6e8dccd41859a813dfd3904aee2\"," +
                "\"context\":{" +
                "\"head_color\":\"#85be53\"," +
                "\"url\":\"https://ejiakanghu.com.cn/guanwang/order_detail_desc.html\"," +
                "\"action_name\":\"查看详情\"," +
                "\"keyword1\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+borrowTime+"\"" +
                "        }," +
                "\"keyword2\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+borrowAddr+"\"" +
                "        }," +
                "\"keyword3\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+returnTime+"\"" +
                "        }," +
                "\"keyword4\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+returnAddr+"\"" +
                "        }," +
                "\"keyword5\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+borrowOrderCode+"\"" +
                "        }," +
                "\"keyword6\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\""+SERVICE_PHONE+"\"" +
                "        }," +
                "\"first\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\"您好，您已成功归还" +num+"轮椅\""+
                "        }," +
                "\"remark\":{" +
                "\"color\":\"#85be53\"," +
                "\"value\":\"谢谢使用。\"" +
                "        }" +
                "      }" +
                "    }" +
                "  }";
        AlipayOpenPublicMessageSingleSendResponse response = sendNoticeMsg(content);
        if(response.isSuccess()){
            logger.debug("leaseReturnSuc调用成功,{}",borrowOrderCode);
        } else {
            logger.error("leaseReturnSuc调用失败,{}",borrowOrderCode);
        }
    }

    public static void main(String[] args) throws AlipayApiException {
//        AliMessageUtil.sendLeaseSuccessMsg("2088802758794372","2018-07-05 17:00:00","test医院","2018072223532910626");
        AliMessageUtil.returnSuccessMsg("2088802758794372","2018-07-05 17:00:00","test医院","2018-07-06 17:00:00","test医院","2018072223532910626","111");
    }
}
